package org.sanguine.study.hometasks.first;

import java.util.Date;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.text.DateFormat;

@SuppressWarnings("deprecation")
public class Runner {
	private static final String AUTHOR = "Alexey Kuznetsov";
	private static final Date DATE_OF_BEGIN = new Date(2014-1900, 3, 28, 15, 03);
	private final TaskWorker worker = getWorkerInstance();
	private final Reader inputReader = createInputReader();
	private final PrintWriter outputWriter = createOutputWriter();
	
	public static void main(String[] args) {
		Runner runner = new Runner();
		runner.run(args);
	}

	public void run(String[] args) {
		if (worker != null) {
			if (worker.setUp(inputReader, outputWriter, args)) {
				worker.doTask();
				printTimeStamp();
				outputWriter.close();
				try {
					inputReader.close();
				} catch (IOException e) {
					System.err.println("IO error");
					e.printStackTrace();
				}
			} else {
				System.err.println("Setup failed");
				printUsage();
			}
		} else {
			System.err.println("Stub worker.  Do nothing.  Please implement right main() method.");
		}
	}

	protected TaskWorker getWorkerInstance() {
		return null;
	};

	protected PrintWriter createOutputWriter() {
		return new PrintWriter(System.out, true);
	}
	protected Reader createInputReader() {
		return new BufferedReader(new InputStreamReader(System.in));
	}

	protected void printTimeStamp() {
		System.out.println("AUTHOR: " + AUTHOR
							+ " DATA OF BEGIN: " + DateFormat.getDateTimeInstance().format(DATE_OF_BEGIN)
							+ " DATE OF COMPLETE: " + DateFormat.getDateTimeInstance().format(new Date()));
	}

	protected void printUsage() {
		System.out.println("USAGE: java -jar runner args1 args2 ...");
	}

}
